#!/usr/bin/env bash

ruby_install_baseruby_check()
{
  __rvm_ensure_has_mri_ruby || return $?
  \typeset compatible_baseruby
  if
    compatible_baseruby="$rvm_wrappers_path/$(__rvm_mri_ruby)/ruby" &&
    [[ -x "$compatible_baseruby" ]]
  then
    rvm_configure_flags+=( --with-baseruby="$compatible_baseruby" )
  else
    return $?
  fi
}

ruby_install()
{
  \typeset result temp_flags _iterator

  case ${rvm_ruby_string:-""} in
    ruby-1.8.*-head)
      __rvm_ensure_has_mri_ruby "ree-|ruby-1.8.|ruby-1.8.7-p|ruby-1.8.7-p374" || return $?
      ;;
    ruby-1.8.*)
      if (( ${rvm_head_flag:-0} == 1 ))
      then __rvm_ensure_has_mri_ruby "ree-|ruby-1.8.|ruby-1.8.7-p|ruby-1.8.7-p374" || return $?
      fi
      ;;
    ruby*-head)
      ruby_install_baseruby_check || return $?
      ;;
    ruby*)
      if (( ${rvm_head_flag:-0} == 1 ))
      then ruby_install_baseruby_check || return $?
      fi
      ;;
  esac

  # TODO: needed on some 1.9.3, confirm with http://bugs.ruby-lang.org/issues/6903
  if
    [[ " ${rvm_configure_flags[*]}" == *" --enable-load-relative"* ]]
  then
    case ${rvm_ruby_string} in
      ruby-1.9.3-p125|ruby-1.9.3-p286|ruby-1.9.3-p327|ruby-1.9.3-p362|ruby-1.9.3-p374|ruby-1.9.3-p385)
        rvm_patch_names+=( ruby-multilib )
        ;;
    esac
  fi

  (
    __rvm_setup_compile_environment "${rvm_ruby_string}" &&
    __rvm_install_source $* &&
    ruby_install_warn_clang
  ) || return $?

  ruby_install_warnings
}

ruby_install_warn_clang()
{
  if
    ! __ruby_clang_ok ${rvm_ruby_string} &&
    [[ -n "${initially_selected_compiler:-}" ]] &&
    __rvm_compiler_is_llvm "${initially_selected_compiler:-}"
  then
    rvm_warn "Ruby '${rvm_ruby_string}' was built using clang - but it's not (fully) supported, expect errors."
  fi
}

ruby_install_warnings()
{
  \typeset patches_count
  patches_count=$(
    rvm_ruby_string="${rvm_ruby_string}" lookup_patchset default | wc -l
  )

  \typeset _default _version _patch _memo _unmaintained_ruby _unmaintained_date
  _unmaintained_ruby=$(__rvm_db "ruby_unmaintained_version")
  _default=$(__rvm_db "default_ruby")
  _version=$(__rvm_db "${_default}_version")
  _patch=$(__rvm_db "${_default}_${_version}_patch_level")
  _version="${_default}-${_version}${_patch:+-}${_patch:-}"
  _memo="Please consider upgrading to ${_version} which will have all of the latest security patches."
  if
    (( ${rvm_head_flag:-0} == 0 )) &&
    [[ "$rvm_ruby_version" != "head" ]] &&
    __rvm_version_compare "$rvm_ruby_version" -le "${_unmaintained_ruby}"
  then
    _unmaintained_date=$(__rvm_db "ruby_unmaintained_date")
    if
      __rvm_version_compare "$rvm_ruby_version" -eq "${_unmaintained_ruby}" &&
      __rvm_version_compare "$(date +%F)" -lt "${_unmaintained_date}"
    then
      rvm_error "WARNING: Please be aware that you just installed a ruby that will finish normal maintenance on ${_unmaintained_date}, for a list of maintained rubies visit:

    http://bugs.ruby-lang.org/projects/ruby/wiki/ReleaseEngineering

${_memo}"
    else
      rvm_error "WARNING: Please be aware that you just installed a ruby that is no longer maintained (${_unmaintained_date}), for a list of maintained rubies visit:

    http://bugs.ruby-lang.org/projects/ruby/wiki/ReleaseEngineering

${_memo}"
    fi
  elif
    [[ "${rvm_ruby_string}" != *"${_version}"* ]] && (( patches_count > 0 ))
  then
    rvm_warn "Please be aware that you just installed a ruby that requires ${patches_count} patches just to be compiled on an up to date linux system.
This may have known and unaccounted for security vulnerabilities.
${_memo}"
  fi
  if (( ${rvm_docs_flag:-0} == 1 ))
  then rvm_log "Ruby was built with documentation, to read it try: ri String"
  else rvm_log "Ruby was built without documentation, to build it run: rvm docs generate-ri"
  fi
}

__rvm_post_configure_ruby()
{
  __rvm_post_configure_ruby_fix_athlon_sse2
  __rvm_post_configure_ruby_update_setup
}

__rvm_post_configure_ruby_fix_athlon_sse2()
{
  if
    [[ -f /proc/cpuinfo && -f Makefile ]] &&
    __rvm_grep "^XCFLAGS = .*-msse2" Makefile >/dev/null &&
    ! __rvm_grep "^flags.*sse2" /proc/cpuinfo
  then
    __rvm_sed_i Makefile -e '/^XCFLAGS =/s/-msse2//'
  fi
}

__rvm_post_configure_ruby_update_setup()
{
  \typeset option
  if
    (( ${rvm_static_flag:-0} == 1 )) && (( ${rvm_dynamic_extensions_flag:-0} == 0 ))
  then
    for option in 'option nodynamic' openssl psych zlib readline
    do
      __rvm_update_ext_setup ${option}
    done
  fi
}

__rvm_update_ext_setup()
{
  echo "extension setup: $1"
  if
    __rvm_grep "$1" ext/Setup >/dev/null
  then
    __rvm_sed_i ext/Setup -e "s/#$1/$1/"
  else
    echo "$1" >> ext/Setup
  fi
}
